﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Entity;
using System.Web.Mvc;

namespace HdipCricketClub.Models
{
    public class OpponentFixturePlayerRepository
    {
        CricketClubEntities db = new CricketClubEntities();

        public List<OpponentFixturePlayer> CreateOpponentFixtureTeam(Fixture fixture)
        {
            List<OpponentFixturePlayer> opponentTeam = new List<OpponentFixturePlayer>();

            for (int i = 1; i <= fixture.no_of_players; i++)
            {
                OpponentFixturePlayer player = new OpponentFixturePlayer()
                                               {
                                                   op_fixture_id = fixture.fixture_id,
                                                   op_batsman_number = i
                                               };
                opponentTeam.Add(player);
            }

            return opponentTeam;
        }

        public OpponentFixturePlayer GetPlayer(int fixtureId, int batsmanNo)
        {
            return db.OpponentFixturePlayers.Include("DismissalType").Include("Fixture").Include("Member").Include("Member1")
                                            .Where(ofp => ofp.op_fixture_id == fixtureId && ofp.op_batsman_number == batsmanNo).Single();
        }

        public List<OpponentFixturePlayer> GetOpponentBatsmen(Fixture fixture)
        {
            return db.OpponentFixturePlayers.Include("DismissalType").Include("Fixture").Include("Member").Include("Member1")
                                            .Where(ofp => ofp.op_fixture_id == fixture.fixture_id)
                                            .OrderBy(ofp => ofp.op_batsman_number).ToList();
        }

        public List<OpponentFixturePlayer> GetOpponentBowlers(Fixture fixture)
        {
            return db.OpponentFixturePlayers.Include("DismissalType").Include("Fixture").Include("Member").Include("Member1")
                                            .Where(ofp => ofp.op_fixture_id == fixture.fixture_id && ofp.op_bowler_number != null)
                                            .OrderBy(ofp => ofp.op_bowler_number).ToList();
        }

        public List<int> GetBowlerIds(List<OpponentFixturePlayer> opponentBowlers)
        {
            List<int> bowlerIds = new List<int>();

            foreach (OpponentFixturePlayer bowler in opponentBowlers)
            {
                bowlerIds.Add(bowler.op_batsman_number);
            }

            return bowlerIds;
        }

        public double GetTotalBattingRuns(List<OpponentFixturePlayer> opponentBatsmen)
        {
            double totalRuns = 0;

            foreach (OpponentFixturePlayer batsman in opponentBatsmen)
            {
                if (batsman.op_batting_runs != null)
                {
                    totalRuns += (double)batsman.op_batting_runs;
                }
            }

            return totalRuns;
        }

        public List<OpponentFixturePlayer> ParseFormCollection(FormCollection formCollection)
        {
            string[] fixtureId = formCollection.GetValues("item.op_fixture_id");
            string[] batsmanNumber = formCollection.GetValues("item.op_batsman_number");
            string[] playerName = formCollection.GetValues("item.op_player_name");
            string[] bowlerNumber = formCollection.GetValues("item.op_bowler_number");

            List<OpponentFixturePlayer> players = new List<OpponentFixturePlayer>();


            for (int i = 0; i < batsmanNumber.Length; i++)
            {
                int playerFixtureId = Int32.Parse(fixtureId[i]);
                int playerBatsmanNumber = Int32.Parse(batsmanNumber[i]);

                OpponentFixturePlayer player = new OpponentFixturePlayer()
                {
                    op_fixture_id = playerFixtureId,
                    op_batsman_number = playerBatsmanNumber
                };

                if (playerName[i] != "")
                {
                    player.op_player_name = playerName[i];
                }

                if (bowlerNumber[i] != "")
                {
                    player.op_bowler_number = Int32.Parse(bowlerNumber[i]);
                }

                players.Add(player);
            }

            List<OpponentFixturePlayer> sortedPlayers = players.OrderBy(ofp => ofp.op_batsman_number).ToList();

            return sortedPlayers;
        }

        public List<OpponentFixturePlayer> ParseFormCollectionBatOnly(FormCollection formCollection)
        {
            string[] fixtureId = formCollection.GetValues("item.op_fixture_id");
            string[] batsmanNumber = formCollection.GetValues("item.op_batsman_number");
            string[] playerName = formCollection.GetValues("item.op_player_name");
            
            List<OpponentFixturePlayer> players = new List<OpponentFixturePlayer>();


            for (int i = 0; i < batsmanNumber.Length; i++)
            {
                int playerFixtureId = Int32.Parse(fixtureId[i]);
                int playerBatsmanNumber = Int32.Parse(batsmanNumber[i]);

                OpponentFixturePlayer player = new OpponentFixturePlayer()
                {
                    op_fixture_id = playerFixtureId,
                    op_batsman_number = playerBatsmanNumber
                };

                if (playerName[i] != "")
                {
                    player.op_player_name = playerName[i];
                }
                               

                players.Add(player);
            }

            List<OpponentFixturePlayer> sortedPlayers = players.OrderBy(ofp => ofp.op_batsman_number).ToList();

            return sortedPlayers;
        }

        public List<OpponentFixturePlayer> GetOpponentPlayers(int id)
        {
            return db.OpponentFixturePlayers.Where(ofp => ofp.op_fixture_id == id).ToList();
        }


    }
}